<template>
<div>
 <div>
   <el-date-picker
     v-model="time"
     type="month"
     placeholder="选择年份和月份"
     @change="timeChange"
     style="margin-left: -750px;">
 </el-date-picker>
   <el-button type="primary" plain style="margin-left: 5px;" @click="search">查询</el-button></div>
  <div style="width: 40%;float:left; margin-left:-30px;">
    <ul style="text-align: left;list-style: none">
      当月销售情况：
      <li>
        当月销售商品数量：{{salesNum}}（个）
      </li>
      <li>
        当月销售额：{{salesAmount}}（元）
      </li>
      <li>
        当月销售利润：{{profit}}（元）
      </li>
  </ul>
  </div>
  <div style="width: 40%;float:left;">
    <ul style="text-align: left;list-style: none">
      当年销售情况：
      <li>
        当年销售商品数量：{{salesNumYear}}（个）
      </li>
      <li>
        当年销售额：{{salesAmountYear}}（元）
      </li>
      <li>
        当年销售利润：{{profitYear}}（元）
      </li>
    </ul>
  </div>
  <el-table
      ref="multipleTable"
      :data="goodsTableDate"
      tooltip-effect="dark"
      style="width: 100%;float: none">
    <el-table-column
        prop="name"
        label="物品名称"
        width="150">
    </el-table-column>
    <el-table-column
        prop="num"
        label="商品数量"
        width="150">
    </el-table-column>
    <el-table-column
        prop="costPrice"
        label="商品进价"
        width="120">
    </el-table-column>
    <el-table-column
        prop="salePrice"
        label="商品售价">
    </el-table-column>
  </el-table>
  <!--    分页栏-->
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
  </el-pagination>
</div>

</template>

<script>
import filter from "@/assets/filter/filter";

export default {
  name: "operationalData",
  data(){
    return{
      historicalOrdersTableDate:[],
      goodsTableDate:[],
      goodsTableDataYear:[],
      time:'',
      month:'',
      year:'',
      salesNum:0,
      salesAmount:0,
      profit:0,
      salesNumYear:0,
      salesAmountYear:0,
      profitYear:0,
      curPage:1,
      pageSize:5,
      total:0
    }
  },
  created() {
    let time=new Date()
    this.month = time.getMonth()+1;
    this.year = time.getFullYear();
    this.loadHistorical()
    this.loadHistoricals()
  },
  methods:{
    async loadHistorical(){
      let item = sessionStorage.getItem("shopId");
      let shopName = item.substring(item.lastIndexOf(",")+1);
      let res =await this.$axios.get('/historicalOrders/queryByShopName',{
        params:{
          shopName:shopName,
          month:this.month,
          year:this.year,
          curPage:this.curPage,
          pageSize:this.pageSize
        }
      })
      this.historicalOrdersTableDate=res.data.data.historicalOrdersKey
      this.goodsTableDate=res.data.data.goodsKey
      this.total=res.data.data.goodsKey.length
      for (let i = 0; i < this.goodsTableDate.length; i++) {
        this.salesNum+=this.goodsTableDate[i].num
        this.salesAmount+=this.goodsTableDate[i].num*this.goodsTableDate[i].salePrice
        this.profit+=this.goodsTableDate[i].num*(this.goodsTableDate[i].salePrice-this.goodsTableDate[i].costPrice)
      }
    },
    async loadHistoricals(){
      let item = sessionStorage.getItem("shopId");
      let shopName = item.substring(item.lastIndexOf(",")+1);
      let res =await this.$axios.get('/historicalOrders/queryByShopNameYear',{
        params:{
          shopName:shopName,
          year:this.year,
          curPage:this.curPage,
          pageSize:this.pageSize
        }
      })
      this.historicalOrdersTableDate=res.data.data.historicalOrdersKey
      this.goodsTableDataYear=res.data.data.goodsKey
      this.total=res.data.data.goodsKey.length
      for (let i = 0; i < this.goodsTableDataYear.length; i++) {
        this.salesNumYear+=this.goodsTableDataYear[i].num
        this.salesAmountYear+=this.goodsTableDataYear[i].num*this.goodsTableDataYear[i].salePrice
        this.profitYear+=this.goodsTableDataYear[i].num*(this.goodsTableDataYear[i].salePrice-this.goodsTableDataYear[i].costPrice)
      }
    },
    search(){
      this.salesNum=0
      this.salesAmount=0
      this.profit=0
      this.salesNumYear=0
      this.salesAmountYear=0
      this.profitYear=0
      this.loadHistorical()
      this.loadHistoricals()
    },
    timeChange(){
      let date =  filter.formatDate(this.time)
      console.log(date)
      let strings = date.split(" ")[0].split('-');
      this.year=strings[0]
      this.month=strings[1]
    },
    handleSizeChange(val) {
      this.pageSize=val
      this.loadHistorical()
      this.loadHistoricals()
    },
    handleCurrentChange(val) {
      this.curPage=val
      this.loadHistorical()
      this.loadHistoricals()
    }
  }
}
</script>

<style scoped>

</style>